﻿<%@ Page Title="SearchListings" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeBehind="SearchListings.aspx.cs" Inherits="RoomService.Listings.SearchListings" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    
    <script type="text/javascript">

        var GetPersURl = "http://23.23.80.142/RoomServiceWCF/pers/5";
        var GetListingsURl = "http://23.23.80.142/RoomServiceWCF/listing";


        function createDynamicTable(tbody, rows, cols, filtered_listing) {
            var min = $('#min').val();
            var max = $('#max').val();
            var i;

            var Rate = new Array(filtered_listing.Length);
            var Title = new Array(filtered_listing.Length);
            var City = new Array(filtered_listing.Length);
            var State = new Array(filtered_listing.Length);
            var Street_address = new Array(filtered_listing.Length);

            for (i = 0; i < filtered_listing.length; i++) {
                
                Rate[i] = filtered_listing[i].listing_rate;
                Title[i] = filtered_listing[i].listing_title;
                City[i] = filtered_listing[i].Address.city;
                State[i] = filtered_listing[i].Address.state;
                Street_address[i] = filtered_listing[i].Address.street_address_1;

                //alert(Street_address[i]);
            }

            
            if (tbody == null || tbody.length < 1) return;
            for (var r = 1; r <= rows; r++) {
                
               var trow = $("<tr>");
               for (var c = 1; c <= cols; c++) {
                   //alert(Rate[r - 1]);
                   var cellRate = Rate[r-1];
                   $("<td>")
                           .addClass("tableCell")
                          .text(cellRate)
                          .data("col", c)
                          .appendTo(trow);
                   //alert(Rate[r]);
                   var cellTitle = Title[r-1]
                   //alert(addr);
                   $("<td>")
                           .addClass("tableCell")
                          .text(cellTitle)
                          .data("col", c)
                          .appendTo(trow);
                   var cellAddress = Street_address[r - 1]
                   //alert(addr);
                   $("<td>")
                           .addClass("tableCell")
                          .text(cellAddress)
                          .data("col", c)
                          .appendTo(trow);
                   var cellCity = City[r - 1]
                   //alert(addr);
                   $("<td>")
                           .addClass("tableCell")
                          .text(cellCity)
                          .data("col", c)
                          .appendTo(trow);
                   var cellState = State[r - 1]
                   //alert(addr);
                   $("<td>")
                           .addClass("tableCell")
                          .text(cellState)
                          .data("col", c)
                          .appendTo(trow);
                   
              }
              trow.appendTo(tbody);
          }
      }

      function withinPriceRange(list_rate) {
            var min = $('#min').val();
            var max = $('#max').val();
            if (max < list_rate) {
                return false;
            }
            if (min > list_rate) {
                return false;
            }
            return true;
      }

      $(document).ready(function () {
          $.ajax({
              cache: false,
              type: "GET",
              async: false,
              dataType: "json",
              url: GetListingsURl,
              success: function (listing) {
                  //alert("success");
              },
              error: function (xhr) {
                  //alert(xhr.statusText);
              }
          });

          $("#get_listing").click(function () {

              $.ajax({
                  cache: false,
                  type: "GET",
                  async: false,
                  url: GetListingsURl,
                  contentType: "application/json",
                  dataType: "json",
                  success: function (listing) {

                      var array_size = listing.length;
                      var filtered_out_values = 0;
                      for (i = 0; i < array_size; i++) {
                          if ((listing[i].listing_rate != null) && (withinPriceRange(listing[i].listing_rate))) {
                              //listing[i] = null;
                              //alert(listing[i].Address.city);
                              filtered_out_values++;
                          }
                      }

                      //alert(filtered_out_values);
                      var filtered_listing = new Array(filtered_out_values);
                      
                      //
                      for (i = 0; i < filtered_listing.length; i++) {
                          for (var j = 0; j < listing.length; j++) {
                              if ((listing[j].listing_rate != null) && (withinPriceRange(listing[j].listing_rate))) {
                                  filtered_listing[i] = listing[j];
                                  // sqft_array[i] = listing[j].
                                  listing[j] = 0;
                                  break;
                              }
                          }
                      }

                      //filtered_listing.Address = listing.Address;
                      createDynamicTable($("#tbl"), filtered_listing.length, 1, filtered_listing);

                  },
                  error: function (xhr) {
                      alert("On Click");
                  }
              });
          });
      });
</script>


    <section id="forms">
  <div class="page-header">
    <h1>Search for a Room</h1>
  </div>
  <form>
        <fieldset class="form-horizontal well span5 offset5">
          <legend>Search</legend>
          <table>
            <tr class="control">
                <td>Zipcode: </td>
                <td><input id="zip" type="text" /></td>
            </tr>
            <tr>
                <td> Min Rate: </td>
                <td><input id="Text1" type="text" /></td>
            </tr>
            <tr>
                <td>Max Rate: </td>
                <td><input id="Text2" type="text" /></td>
            </tr>
            <tr>
                <td>Housing Type: </td>
                <td><select id="housingtype">
                            <option>Apartment</option>
                            <option>Condo</option>
                            <option>Dormatory</option>
                            <option>House</option>
                            <option>Townhome</option>
                          </select></td>
            </tr>
            <tr>
                <td>Available Beds: </td>
                <td><select id="avlbeds">
                            <option>1</option>
                            <option>2</option>
                            <option>3</option>
                            <option>4</option>
                            <option>5+</option>
                          </select></td>
            </tr>
             <tr>
                <td> Bath Type: </td>
                <td><select id="bathtype">
                            <option>Shared</option>
                            <option>Individual</option>
                     </select></td>
            </tr>
            <tr>
                <td> Total Beds</td>
                 <td><select id="totbeds">
                            <option>1</option>
                            <option>2</option>
                            <option>3</option>
                            <option>4</option>
                            <option>5+</option>
                          </select></td>
            </tr>
             <tr>
                <td>Pool: <input type="checkbox" id="pool" value="option1"></td>
                <td>Laundry: <input type="checkbox" id="laundry" value="option1"></td>
            </tr>
             <tr>
                <td>Gym: <input type="checkbox" id="gym" value="option1"></td>
                <td>Furnished: <input type="checkbox" id="furnished" value="option1"></td>
            </tr>
             <tr>
                <td>Internet: <input type="checkbox" id="internet" value="option1"></td>
                <td>Cable: <input type="checkbox" id="cable" value="option1"></td>
            </tr>
            
          </table>
          
          <div>
          <button id="Button1">Refresh Search</button>
          </div>
     </fieldset>
   </form>
  <form>
        
    <form>
        <fieldset class="form-horizontal well span8 offset4">
        <asp:Button id="get_listing">Get Listings</button>
        <table class="table table-bordered table-striped" width="100" id="tbl" border="1">


        <tr>
         
 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
            DataSourceID="SqlDataSource3"
            CssClass="table table-striped table-bordered table-condensed"
            >
            <Columns>
                <asp:BoundField DataField="column1" HeaderText="Number" 
                    SortExpression="column1" />
                <asp:BoundField DataField="rent" HeaderText="Listing Price" SortExpression="rent" />
                <asp:BoundField DataField="TotalBeds" HeaderText="Total Beds" 
                    SortExpression="TotalBeds" />
                <asp:BoundField DataField="avlBeds" HeaderText="avlBeds" 
                    SortExpression="avlBeds" />
                <asp:BoundField DataField="bathType" HeaderText="bathType" 
                    SortExpression="bathType" />
                <asp:BoundField DataField="startDate" HeaderText="startDate" 
                    SortExpression="startDate" />
            </Columns>

          </asp:GridView>
 
        </fieldset>
    </form>
     
        <asp:SqlDataSource ID="SqlDataSource3" runat="server" 
            ConnectionString="<%$ ConnectionStrings:gsuhousingConnectionString %>" 
            SelectCommand="SELECT [listing#] AS column1, [rent], [TotalBeds], [avlBeds], [bathType], [startDate] FROM [Listings]"
            >
           
          
        </asp:SqlDataSource>

  <asp:Table ID = "table1" runat="server" class="table table-bordered table-striped" padding="300">
            
            </asp:Table>

   <asp:label id="lblMessage" runat="server" width="288px" font-bold="True" font-italic="True"
            font-size="Medium" forecolor="#C00000"></asp:label>
     
</form>
    </form>
    </form>
</asp:Content>
